﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace Futaba.SBus.Telemetry.Configuration
{
	/// <summary>
	/// Specifies the definition of a value/slot sent by a <see cref="Sensor">sensor</see>
	/// </summary>
	public class SensorParameter
	{
		/// <summary>
		/// Creates a default instance of this class
		/// </summary>
		public SensorParameter()
		{
			ParsingFlags = SensorValueParsingFlags.Default;
			Position = 1;
			ConsumesSlots = 1;
			ValueMultiplyer = 1;
		}
		/// <summary>
		/// The name of the slot as it appears in the converted CSV telemetry file
		/// </summary>
		[XmlAttribute("parameterName")]
		public string ParameterName { get; set; }
		/// <summary>
		/// A human readable display name of this sensor
		/// </summary>
		[XmlAttribute("displayName")]
		public string DisplayName { get; set; }
		/// <summary>
		/// The unit of measurement for this value (ie. m, km/h, °c, etc.)
		/// </summary>
		[XmlAttribute("valueLabel")]
		public string ValueLabel { get; set; }
		/// <summary>
		/// A number-format to apply to the slot's current-value, when displaying it (ie #,##0.00)
		/// </summary>
		[XmlAttribute("valueFormat")]
		public string ValueFormat { get; set; }
		/// <summary>
		/// Specifies how to parse the slot-information
		/// </summary>
		[XmlAttribute("parsingFlags")]
		public SensorValueParsingFlags ParsingFlags { get; set; }
		/// <summary>
		/// Specifies the multiplyer applied to the parsed value
		/// </summary>
		[XmlAttribute("valueMultiplyer"), DefaultValue(1D)]
		public double ValueMultiplyer { get; set; }
		/// <summary>
		/// The type of the value
		/// </summary>
		[XmlAttribute("valueType")]
		public SensorValueType ValueType { get; set; }
		/// <summary>
		/// the relative slot within the <see cref="Sensor">sensor</see>.
		/// Multiple values with the same slot must have a different <see cref="SubSlotId">sub-slot identifier</see>
		/// </summary>
		[XmlAttribute("position")]
		public byte Position { get; set; }
		/// <summary>
		/// If set, the first byte of the slot must match this value
		/// Multiple values with the same slot must have a different <see cref="SubSlotId">sub-slot identifier</see>
		/// </summary>
		public byte? SubSlotId { get; set; }
		/// <summary>
		/// The amount of slots consumed to be able to construct this slot's current value
		/// </summary>
		[XmlAttribute("consumesSlots")]
		public int ConsumesSlots { get; set; }

		/// <summary>
		/// Returns an informative string of this instance
		/// </summary>
		public override string ToString()
		{
			return string.Format("{0} ({1}) Position={2}.{3} (ConsumesSlots: {4})", DisplayName, ValueType, Position, (object)SubSlotId ?? "null", ConsumesSlots);
		}
	}
}
